package com.wuyan.locationdog


import androidx.lifecycle.lifecycleScope
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.constant.Constants
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.SPUtils
import com.wuyan.locationdog.databinding.ActivitySplashBinding
import com.wuyan.locationdog.help.LoginHelp
import com.wuyan.locationdog.ui.device.OneDeviceActivity
import com.wuyan.locationdog.ui.login.Login1Activity
import com.wuyan.locationdog.ui.login.LoginActivity
import com.wuyan.locationdog.view.AgreePrivacyDialog
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SplashActivity : BaseBindingActivity<ActivitySplashBinding>() {

    override fun initData() {
        BarUtils.transparentStatusBar(this)
        binding.apply {
            tvVersion.text = "V" + AppUtils.getAppVersionName()
        }
        showAgreePrivacyDialog()
    }

    /**
     * 隐私弹窗
     */
    private fun showAgreePrivacyDialog() {
        if (!SPUtils.getInstance().getBoolean(Constants.SP_IS_AGREE_PRIVACY, false)) {
            AgreePrivacyDialog(mActivity) {
                //隐私同意后做SDK初始化
                MyApplication.agreePrivacyAfterInit()
                goActivity()
            }.showPopupWindow()
        } else {
            lifecycleScope.launch {
                delay(1000)
                goActivity()
            }
        }
    }

    fun goActivity() {
        lifecycleScope.launch {
            if (!SPUtils.getInstance().getString("token").isNullOrEmpty()) {
                if (LoginHelp.isDeviceLogin) {
                    if (SPUtils.getInstance().getBoolean("deviceBindDogSuccess")) {
                        ActivityUtils.startActivity(OneDeviceActivity::class.java)
                    } else {
                        ActivityUtils.startActivity(Login1Activity::class.java)
                    }
                } else {
                    ActivityUtils.startActivity(MainActivity::class.java)
                }
            } else {
                ActivityUtils.startActivity(Login1Activity::class.java)
            }
            finish()

        }
    }

    override fun observe() {
    }
}